package com.ruoyi.teacher.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.teacher.domain.Review;
import com.ruoyi.teacher.domain.Student;
import com.ruoyi.teacher.mapper.ReviewRepository;
import com.ruoyi.teacher.mapper.StudentRepository;
import com.ruoyi.teacher.service.ReviewSevice;
import com.ruoyi.teacher.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;   // jdk 代理生成
    @Autowired
    private ReviewRepository reviewRepository;


    @Override
    public void save(Student student) {

        // id 为空说明是添加
        if (student.getId() == null) {
            studentRepository.save(student); //
            return;
        }
        Student save = studentRepository.save(student);
        Review review = new Review();
        review.setName(save.getTeacher());
        review.setFraction(0);
        reviewRepository.save(review);

    }

    @Override
    public void delete(Long id) {
        studentRepository.deleteById(id);
    }

    @Override
    public AjaxResult List() {
        List<Student> all = studentRepository.findAll();
        return AjaxResult.success("成功", all);
    }


}
